$(document).ready(init);

function init() {
	$('#clear').click(cleardata);
		
	$('#add').click(add);
	
	
	initDepartMent();
}

function cleardata(){
	var texts = $('input[type="text"]').each(function() {
		this.value="";
	});
	
	$('#rebate').val(100);
}

function checkDeptNo(sysch) {
	var flag = false;
	
	var option = {
		async : sysch,
   		type: 'POST',
   		url: 'department!checkDeptNoRepete.action',
   		data: {
			'newDeptNo' : $.trim($('#deptNo').val())
   		},
   		success : function(msg){
	      if(msg=='success') {
	      	 flag = true;
	      } else {
	      	$.messager.alert('确定', '<b>仪器编号重复，请重新填写</b>', 'warning');
	      }
		},
		error : function(XMLHttpRequest, textStatus, errorThrown) {
		     $.messager.alert('确定', '<br/><b>很遗憾，检查组别编号重复失败，请稍后重试,状态为</b>'  + textStatus + ', 错误为： ' + errorThrown, 'error');
		}
	}
	
	$.ajax(option); 
	
	return flag;
}

function add() {
	var deptName = $.trim($('#deptName').val());
	var deptNo = $.trim($('#deptNo').val());
	
	if ('' == deptName) {
		$.messager.alert('确定', '<b>机构名称不能为空</b>', 'warning');
		return false;
	}
	
	if ('' == deptNo) {
		$.messager.alert('确定', '<b>机构编号不能为空</b>', 'warning');
		return false;
	}
	
	var rebate = $.trim($('#rebate').val());
	if ('' == rebate) {
		$.messager.alert('确定', '<b>折扣费率不能为空</b>', 'warning');
		$('#rebate').val('100');
		return false;
	}
	
	if ('' != rebate && isNaN(rebate)) {
		$.messager.alert('确定', '<b>折扣费率必须为0到100的整数</b>', 'warning');
		$('#rebate').val('100');
		return false;
	}
	
	if (!checkDeptNo(false)) { 
		return false;
	}
	
	var re = /^[1-9]+[0-9]*]*$/;  
   
    if ((rebate != '') && !re.test(rebate))  
    {
     	$.messager.alert('警告', '折扣费必填是大于0的整数', 'warning');
     	
     	$('#rebate').val('100');
     	
         return false;  
    }  
    
    if (rebate > 100) {
    	$.messager.alert('警告', '折扣费最大值是100', 'warning');
     	
     	$('#rebate').val('100');
     	
         return false;  
    }
	
	var params = {
		'iDepartment.deptName' : deptName,
		'iDepartment.type' : $('#type').val(),
		'iDepartment.phone' : $('#phone').val(),
		'iDepartment.address' : $('#address').val(),
		'iDepartment.fax' : $('#fax').val(),
		'iDepartment.postCode' : $('#postCode').val(),
		'iDepartment.rebate' : rebate,
		'iDepartment.facilityExtent' : $('#facilityExtent').val(),
		'iDepartment.remark' : $('#remark').val(),
		'iDepartment.payCode' : $('#payCode').val(),
		'iDepartment.deptNo' : deptNo,
		'iDepartment.parentID' : $('#parentID').val()
	}
	
	var option = {
   		type: 'POST',
   		url: 'department!addDepartMent.action',
   		data: params,
   		success : function(msg){
	      if(msg=='success') {
	      	 $.messager.alert('确定', '恭喜您操作成功', 'info');
	     	 cleardata();
	     	 initDepartMent();
	      } else {
	     	 $.messager.alert('确定', '操作失败,请稍后执行', 'error');
	      }
		},
		error : function(XMLHttpRequest, textStatus, errorThrown) {
		     $.messager.alert('确定', '<br/><b>很遗憾，操作出现异常,状态为</b>'  + textStatus + ', 错误为： ' + errorThrown, 'error');
		}
	}
	
	$.ajax(option); 
}

function hiddenParentDeptNo() {
	$('#win').window('close');
	     	
	$('#parentID').val(0);
	     	
	$('#parentDeptNo').text("");
}

function showParentDeptNo() {
	$('#win').window("open");
	
	$('#win').window({collapsible:false,
	    minimizable:false,
	    maximizable:false,
	    closable:false,
		closed:false,
		tools:[{
	        iconCls:'icon-cancel',
	        handler:function(){
	        	   if (document.getElementById('parentID').value != '0') {
		        	   	$.messager.confirm("确定", "您是否取消已选从属机构？",function(r) {
							if (r) {
								$('input[name="grand"][value="0"]').attr("checked", "checked");
					     	
					     		$('#parentID').val(0);
					     	
					     		$('#parentDeptNo').text("");
							}
					    })
	        	   } else {
	        	   		$('input[name="grand"][value="0"]').attr("checked", "checked");
	        	   }
	        	   $('#win').window("close");
	        }
   		 }]
	});
}

function initDepartMent() {
	$('#department').datagrid({
	    title:'',
	    iconCls:'icon-save',
	    url:'department!showDepartMent.action',
	    idField: 'deptID',
	    queryParams:{
	    	queryDptType:0
	    },
	    columns:[[
	        {field:'deptName',title:'机构名称',width:100},
	        {field:'deptNo',title:'机构编号',width:100},
	        {field:'payCode',title:'付款账号',width:100},
	        {field:'phone',title:'电话',width:80},
	        {field:'address',title:'联系地址',width:100},
	        {field:'deptID',title:'修改',width:100,align:'center',
	        	    formatter : function(value, row, index) {
							return  '<a href="#" style="color:red;text-decoration:none;" onclick="choose('+ index + ',\'' + row +'\')">选中</a> ';
					}
			}
	    ]],
	     pageList:[10],
	     pagination:true,
	     singleSelect:true,
	     rownumbers: true,
	     fitColumns:true
	});
}


function choose(index, row) {
	$('#department').datagrid('clearSelections');
	$('#department').datagrid('selectRow', index);
	
	var selected = $('#department').datagrid('getSelected');
	
	$('#parentID').val(selected.deptID);
	
	$('#parentDeptNo').text(selected.deptName);
	
	$('#win').window('close');
}
